﻿using Max.PH5_2004A.Shopping.DAL;
using Max.PH5_2004A.Shopping.IRespository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH5_2004A.Shopping.Respository
{
    public class CRUDRespository<T> : ICRUDRespository<T> where T : class,new()
    {
        //依赖注入
        MaxPH52004AShoppingDbContext _context;
        public CRUDRespository(MaxPH52004AShoppingDbContext context)
        {
            _context = context;
        }

        //查询
        public IQueryable<T> Select()
        {
            var list =_context.Set<T>().AsQueryable();

           // _context.Set<T>().or
            return list;
        }
        public int Insert(T model)
        {
            //将model添加到对应的表中（内存）
            _context.Set<T>().Add(model);
            //将内存中的数据提交到数据库
            int result = _context.SaveChanges(); 

            //返回结果
            return result;
        }

        public int Update(T model)
        {
            _context.Set<T>().Update(model);
            int result = _context.SaveChanges();
            return result;

        }
        public int Delete(string id)
        {
            //去找这个id的对象
            var model = _context.Set<T>().Find(id);
            if (model == null)
                return -1;

            _context.Set<T>().Remove(model);
            var result =_context.SaveChanges();
            return result;
        }


    }




}
